#include "common.hpp"
#include "fifo.hpp"

int main()
{
    //得到创建共享内存的key
    key_t server_key = get_shmkey();
    std::cout << "server_key" << to_hexa(server_key) << std::endl;

    //创建共享内存
    int shm_id = creat_shmid(server_key, SHM_SIZE);
    std::cout << "shm_id: " << shm_id << std::endl;

    std::cout << "shm informathin: " << std::endl;
    shm_debug(shm_id);

    //共享内存关联进程
    char* address = (char*)shmattch(shm_id);
    std::cout << "Attch shm succsss, addr: " << to_hexa(uint64_t(address)) << std::endl;

    //引入协同机制
    Fifo fifo;
    Sync sync;
    sync.open_read();
    
    //开始通信
    std::cout << "read" << std::endl;
    while (1)
    {
        std::cout << "shm content: " << address << std::endl;
        sleep(1);
        
        if (!sync.Wait())
            break;
    }

    //取消共享内存和进程的关联
    shm_unattch(address);
    std::cout << "Shm unattch success, address: " << to_hexa(uint64_t(address)) << std::endl;

    //删除共享内存
    shm_delete(shm_id);
    std::cout << "Shm delete success, shm id: " << shm_id << std::endl;
    
    return 0;
}